<template>
    <div>
          <!-- 面包屑导航区域 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>权限管理</el-breadcrumb-item>
            <el-breadcrumb-item>角色列表</el-breadcrumb-item>
           
        </el-breadcrumb>

        <!-- 卡片视图 -->
        <el-card  height="580" >
            <el-row>
                <el-col>
                    <el-button type="primary">添加角色</el-button>
                </el-col>
            </el-row>

            <!-- 角色列表区域 -->
            <el-table :data="rolelist" border stripe >

                <!-- 设置展开列 -->
                <el-table-column type="expand">
                    <!-- 作用域插槽 -->
                    <template slot-scope="scope">
                        <!-- <pre>{{scope.row}}</pre> -->

                        <el-row :class="['dbbottom' , i1 === 0 ?'dbtop' :'' ,'vcenter']" v-for="(item1, i1) in scope.row.children" :key="item1.id">
                            <!-- 渲染一级权限 -->
                            <el-col :span="5">
                                
                                <el-tag closable @close="removeRightsById(scope.row,item1.id)">{{item1.authName}}</el-tag>
                                <i class="el-icon-caret-right"></i>
                            </el-col>
                            <!-- 渲染二级权限 -->
                            <el-col :span="19">
                                <!-- for循环 嵌套渲染 -->
                                <el-row :class="[i2 === 0 ? '':'dbtop' , 'vcenter']" v-for="(item2, i2) in item1.children" :key="item2.id">
                                    <el-col :span="6">
                                        <el-tag type="success"
                                        closable @close="removeRightsById(scope.row,item2.id)">
                                            {{item2.authName}}
                                        </el-tag>
                                        <i class="el-icon-caret-right"></i>
                                    </el-col>
                                    <el-col :span="18">
                                        <el-tag type="warning" v-for="(item3 ) in item2.children" :key="item3.id"
                                        closable @close="removeRightsById(scope.row,item3.id)">
                                            {{item3.authName}}
                                        </el-tag>
                                    </el-col>
                                </el-row>
                            </el-col>
                        </el-row>

                    </template>
                </el-table-column>

                <el-table-column type="index"></el-table-column>
                <el-table-column label="角色名称" prop="roleName"></el-table-column>
                <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
                <el-table-column label="操作" width="450px">
                    <template slot-scope="scope">
                        <!-- 功能待完善 -->
                        <el-button type="primary" v-model="scope.row" icon="el-icon-search">编辑</el-button>
                        <el-button type="primary" icon="el-icon-search">删除</el-button>
                        <el-button type="primary" icon="el-icon-search"
                        @click="showRightsEdit(scope.row)">分配权限</el-button>
                    </template>
                </el-table-column>
                
            </el-table>
        </el-card>


        <!-- 分配权限弹出对话框 -->
        <el-dialog
            title="分配权限"
            :visible.sync="setRightDialogVisible"
            width="50%" @close="setClosed">

            <!-- 树形控件 -->
            <el-tree :data="rightlist" :props="dtreeProps" show-checkbox node-key="id"
            default-expand-all :default-checked-keys="defKeys" ></el-tree>
            
            <span slot="footer" class="dialog-footer">
                <el-button @click="setRightDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="setRightDialogVisible = false">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>

export default {
    data(){
        return {
            // 所有角色列表数据
            rolelist:[],
            // 树形控件数据
            rightlist:[],
            defKeys:[], //树形控件默认展开
            // 控制弹框
            setRightDialogVisible:false,
            // 树形控件
            dtreeProps:{
                label:'authName',
                children:'children'
            }
        }
    },
    created(){
        this.getRolesList()
    },
    methods:{
        // 获取所有角色列表数据
        async getRolesList(){
            const {data:res} = await this.$http.get('roles')

            if(res.meta.status !== 200 ){
                return this.$message.error('获取角色列表失败!')
            }

            this.rolelist = res.data
            // console.log(this.rolelist);
        },
        // 删除权限
        async removeRightsById(role, rightId){
          const resultmsg = await this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then((resCan) => resCan)
        .catch((err) =>err);

        if(resultmsg === 'cancel'){
           return this.$message({
            type: 'info',
            message: '已取消删除'
          });
        }
        const {data:res} = await this.$http.delete(`roles/${role.id}/rights/${rightId}`)

        if(res.meta.status !== 200){
            return this.$message.error('删除权限失败！')
        }
        this.$message({
            type: 'success',
            message: '删除成功!'
          });
        //   会引起全页面重新渲染
        // this.getRolesList()

        // 点击删除传来scope.row，函数接收成为role,此时直接给role.children赋值，引起变量改变，触发数据驱动更新
        role.children = res.data
        },
       
        
        // 分配权限对话框展示
        async showRightsEdit(role){ 
            // 获取所有权限信息
            const {data:res} = await this.$http.get('rights/tree')

            if(res.meta.status !== 200){
                return this.$message.error('获取权限信息失败!')
            }
            // 保存到data中
            this.rightlist = res.data

            // console.log(this.rightlist);

            // 获取三级权限节点的Id
            this.getLeafKeys(role, this.defKeys)

            // 控制弹框
            this.setRightDialogVisible =true
        },
        // 通过递归的形式，获取角色下所有三级权限的id，并且保存到defKeys中
        getLeafKeys(node, arr){
            // 如果当前node节点没有children属性，则是三级权限
            if(!node.children){
                return arr.push(node.id)
            }

            node.children.forEach(item =>{
                this.getLeafKeys(item, arr)
            })
        },
        // 监听对话框关闭事件
        setClosed(){
            this.defKeys = []
        }

    }
}
</script>

<style lang="less" scoped>
.el-tag{
    margin: 7px;
}
.dbtop{
    border-top: 1px solid #eee;
}
.dbbottom{
    border-bottom: 1px solid #eee;
}
.vcenter{
    display: flex;
    align-items: center;
}
</style>